home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / 255FILES.C next >
C/C++ Source or Header  |  1990-10-14  |  2KB  |  71 lines

  1. /*
  2. **  255files.c
  3. **
  4. **  Originally written for Turbo C by Doug Burger
  5. **  11 Mar 90
  6. **
  7. **  Modified for Zortech C++ and Microsoft C/Quick C by Bob Stout
  8. **  13 Oct 90
  9. **
  10. **  Demonstrates relocating the file handle table under DOS 3.x
  11. **  for having more than the usual 20 files open in a single
  12. **  program
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <dos.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19.  
  20. #define TABLE_SIZE 100
  21.  
  22. #if !defined(__ZTC__) && !defined(__TURBOC__)   /* i.e. #if MSC/QC   */
  23.  #include <stdlib.h>
  24.  #define MK_FP(seg,offset) \
  25.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  26.  
  27.  /* MSC's open() is funny - this code only works with _dos_open()    */
  28.  int open(char *name, unsigned mode)
  29.  {
  30.          int hdl;
  31.  
  32.          if (0 == _dos_open(name, mode, &hdl))
  33.                  return hdl;
  34.          else    return -1;
  35.  }
  36. #endif
  37.  
  38. unsigned char handle_table[TABLE_SIZE]; /* table of file DOS handles */
  39. unsigned char far * far * handle_ptr;   /* ptr to DOS's ptr to hand. */
  40. unsigned int far *handle_count;         /* ptr to handle count */
  41.  
  42. void relocate(void)
  43. {
  44.     unsigned int i;
  45.  
  46.     handle_count = MK_FP(_psp, 0x32);   /* handle count at PSP:32h */
  47.     handle_ptr = MK_FP(_psp, 0x34);     /* table ptr at PSP:34h */
  48.     for (i = 0; i < *handle_count; i++) /* relocate exiting table */
  49.         handle_table[i] = (*handle_ptr)[i];
  50.     for (i = *handle_count; i < TABLE_SIZE; i++)    /* init. rest */
  51.         handle_table[i] = 255;
  52.     *handle_ptr = handle_table;         /* set pointer to new table */
  53.     *handle_count = TABLE_SIZE;         /* set new table size */
  54. }   /*  relocate()  */
  55.  
  56. /*********************************************************************/
  57. void main(void)
  58. {
  59.     int c, h;
  60.  
  61.     relocate();
  62.  
  63.     c = 0;
  64.     while ((h = open("CON", O_RDONLY)) >= 0)    /* DOS closes files */
  65.     {
  66.         c++;                                    /* on exit, so I    */
  67.         printf("handle = %d\n", h);             /* don't bother     */
  68.     }                                           /* saving handles   */
  69.     printf("total opened files = %d\n", c);
  70. }   /*  255files.c  */
  71.